package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 证件查询
 * 
 */
public class SearchCard extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;
   private String number = "0";
   private String style = "0";
   private String  contract_id = "0";
   private String  contract_addr = "0";
   private String searchBeginDate = "0";
	private String searchEndDate = "0";
	private String contract_begin = "0";
	private String contract_end = "0";
	
	Button search = new Button("查询");// 查询按钮
	Button export =  new Button("导出报表");
	
	public SearchCard() {
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
	}
	public void complete() {
		super.complete();
		getToolbarll().add(export);
		getToolbarll().add(print);
		print.addListener(Events.OnClick, getListener());
		export.addListener(Events.Select, getListener());
	}
	
	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("查询证件详情");

		fs.add(createForm());
		HorizontalPanel s = new HorizontalPanel();
		s.add(search);
		fs.add(s);

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("departments");
		type.getField("departments").setType(String.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("idNo");
		type.getField("idNo").setType(String.class);
		type.addField("addr");
		type.getField("addr").setType(String.class);
		type.addField("availableDate");
		type.getField("availableDate").setType(Date.class);
		type.addField("passAvailableDate");
		type.getField("passAvailableDate").setType(Date.class);
		type.addField("issue");
		type.getField("issue").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_idCard");
		info.setKeyname(new String[] { "icode" });
		String sql = "select * from hr_idCard  where 1=2 ";
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("bcode", "姓名", 120);
		BaseComboBoxForm<BusiObject> bf = new BaseComboBoxForm<BusiObject>("bcode","employno %2B bname","v_bcode2job","",true);
		C1.setRenderer(bf);
		bf.setAllowBlank(false);
		columns.add(C1);

		ColumnConfig C8 = new ColumnConfig("departments", "所属部门", 80);
		BaseComboBoxForm<BaseModelData> dept = new BaseComboBoxForm<BaseModelData>("bcode","departments","bcode","",true);
		dept.setName("bcode");
		dept.setReadOnly(true);
		dept.setAllowBlank(false);
		columns.add(C8);	
			
		ColumnConfig C7 = new ColumnConfig("type", "证件类型", 80);
		columns.add(C7);

		ColumnConfig C2 = new ColumnConfig("idNo", "证件号", 140);
		TextField<String> c10_t = new TextField<String>();
		C2.setEditor(new CellEditor(c10_t));
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("addr", "地址", 200);
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("availableDate", "有效日期", 85);
	    C4.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C4);
		
		ColumnConfig C5 = new ColumnConfig("passAvailableDate", "签证有效期", 85);
	    C5.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C5);
		
		ColumnConfig C6 = new ColumnConfig("issue", "签发机关", 120);
		columns.add(C6);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.99, 0.65);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
	//	grid.getStore().reload(); // 暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}

	FormPanel form;
	BaseComboBoxForm<BusiObject> name = new BaseComboBoxForm<BusiObject>("bcode","employno %2B bname","v_bcode2job","",true);
	TextField<String> numbers = new TextField<String>(); // 证件号码
	TextField<String> address = new TextField<String>(); // 证件地址
	BaseComboBoxX type = new BaseComboBoxX(
			"name", "dictionary", "where type='hr_cardtype' and name != '健康证'");
	// 证件类型
	DateField availableDateStart = new DateField(); // 有效开始日期
	DateField availableDateEnd = new DateField(); // 有效结束日期
	DateField passAvailableStart = new DateField(); // 签证开始日期
	DateField passAvailableEnd = new DateField(); // 签证结束日期

	private FormPanel createForm() {
		form = new FormPanel();

		availableDateStart.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		availableDateEnd.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		passAvailableStart.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		passAvailableEnd.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));

		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("人员姓名:"), td);
		form.add(name, td);
		
		form.add(new Label("证件类型:"), td);
		form.add(type, td);

		form.add(new Label("证件编号:"), td);
		form.add(numbers, td);

		form.add(new Label("证件地址:"), td);
		form.add(address, td);

		form.add(new Label("有效日期从:"), td);
		form.add(availableDateStart, td);

		form.add(new Label("到:"), td);
		form.add(availableDateEnd, td);

		form.add(new Label("签证日期从:"), td);
		form.add(passAvailableStart, td);

		form.add(new Label("到:"), td);
		form.add(passAvailableEnd, td);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;
	String searchSql = "";
	String sqlExcel = "";
	
	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
	
				String where = "where bcode.bcode =hr_idCard.bcode and exists(select 1 from bcode_job where employstatus = '在职' and bcode = hr_idCard.bcode)  ";
			if(be.getSource().equals(search)){

				if (!name.getRawValue().equals("")) {
				    number = name.getValueCode();
					where += "and hr_idCard.bcode  like '%25" + name.getValue().get("code") + "%25'";
				}else {
					  number = "0";
				}
				
				if (!numbers.getRawValue().equals("")) {
					contract_id = numbers.getRawValue();
					where += " and hr_idCard.idNo like '%25"
						+ numbers.getRawValue() + "%25'";
				}else{
					contract_id = "0";
				}
				   
				if (!address.getRawValue().equals("")) {
					contract_addr = address.getRawValue();      
					where += " and addr like '%25"
						+ address.getRawValue() + "%25'";
				}else {
					contract_addr = "0";
				}
				if (!type.getRawValue().equals("")) {
                    
					style = type.getValue().get("code");
					where += " and type = '"
						+ type.getValue().get("code") + "'";
				}else{
					style = "0";
				}
				
				if (!availableDateStart.getRawValue().equals("")) {
					searchBeginDate = availableDateStart.getRawValue();
					where += " and availableDate >= '"
						+ availableDateStart.getRawValue() + "'";
				} else{
					searchBeginDate = "0";
				}
				if (!availableDateEnd.getRawValue().equals("")) {
					searchEndDate  = availableDateEnd.getRawValue();
					where += " and availableDate <= '"
						+ availableDateEnd.getRawValue() + "'";
				}else{
					searchEndDate = "0";
				}
				if (!passAvailableStart.getRawValue().equals("")) {
					contract_begin = passAvailableStart.getRawValue();
					where += " and passAvailableDate >= '"
						+ passAvailableStart.getRawValue() + "'";
				}else{
					contract_begin = "0";
				}
				if (!passAvailableEnd.getRawValue().equals("")) {
					contract_end = passAvailableEnd.getRawValue();
					where += " and passAvailableDate <= '"
						+ passAvailableEnd.getRawValue() + "'";
				}else{
					contract_end = "0";
				}
			
		String	searchSql = "select hr_idCard.icode,hr_idCard.bcode,type,hr_idCard.idNo,addr,availableDate,passAvailableDate,issue,departments from hr_idCard ,bcode  " + where + " order by availableDate asc";
		
				grid.getStore().getStoreinfo().setSql(searchSql);
				grid.getStore().reload();
			}else{
				
						StringBuilder data = new StringBuilder();
						data.append("number=");
						data.append(number);
						data.append("&contract_id=");
						data.append(contract_id);
						data.append("&contract_addr=");
						data.append(contract_addr);
						data.append("&style=");
						data.append(style);
						data.append("&searchBeginDate=");
						data.append(searchBeginDate);
						data.append("&searchEndDate=");
						data.append(searchEndDate);
						data.append("&contract_begin=");
						data.append(contract_begin);
						data.append("&contract_end=");
						data.append(contract_end);
						
						if (be.getSource().equals(export)) {
							data.append("&excel=1");
						}
				
				Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/SearchCard.jsp?" + data, "_blank", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
		}
			}
			
			};
		return defaultlistener;
	}
}
